﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Data.SqlClient;
using System.Data.SqlServerCe;
using ZedGraph;
using System.Text.RegularExpressions;

namespace Trade_Recorder
{
    public partial class TRMain : Form
    {
        private string Version = "0.9a";

        public TRMain()
        {
            InitializeComponent();
        }

        private void TRMain_Load(object sender, EventArgs e)
        {            
            // TODO: This line of code loads data into the 'tradesDataSet.Trades' table. You can move, or remove it, as needed.
            this.tradesTableAdapter.Fill(this.tradesDataSet.Trades);      
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {            
            deleteRow();           
        }        
        
        private void deleteRow()
        {
            DialogResult dr = MessageBox.Show("Are you sure you want to delete this row?", "Confirmation", 
                MessageBoxButtons.YesNo, MessageBoxIcon.Question);

            if (dr == DialogResult.Yes)
            {
                while (dataGridView1.SelectedRows.Count > 0)                    
                    dataGridView1.Rows.Remove(dataGridView1.SelectedRows[0]);                
                                
                try
                {
                    this.Validate();
                    this.tradesBindingSource.EndEdit();
                    this.tradesTableAdapter.Update(this.tradesDataSet.Trades);
                }
                catch (Exception ex)
                {
                    MessageBox.Show("An error occurred during the update process: " + ex);
                    // Add code to handle error here.
                }

                this.tradesTableAdapter.Fill(this.tradesDataSet.Trades);
                
            }
        }
        
        public void computeTwoDigitValues()
        {
            
            // Convert our Entry and Exit prices to a float
            float entryPL = float.Parse(textBoxEntryPrice.Text);
            float exitPL = float.Parse(textBoxFinalExitPrice.Text);
            
            // If the position is long, we have to do custom            
            switch (comboBoxPosition.Text)
            {
                case "Long":
                    string convertedLongValue = Convert.ToString(Math.Round(exitPL - entryPL, 2) * 100);
                    
                    TradesDataSet.TradesRow newTradesRow = tradesDataSet.Trades.NewTradesRow();

                    newTradesRow.Datestamp = textBoxDate.Text;
                    newTradesRow.Instrument = comboBoxInstrument.Text;
                    newTradesRow.Position = comboBoxPosition.Text;
                    newTradesRow.Strategy = comboBoxStrategy.Text;
                    newTradesRow.EntryPrice = textBoxEntryPrice.Text;
                    newTradesRow.FinalExitPrice = textBoxFinalExitPrice.Text;
                    newTradesRow.Pips = convertedLongValue;
                    tradesDataSet.Trades.Rows.Add(newTradesRow);
                    break;
                case "Short":
                    string convertedShortValue = Convert.ToString(Math.Round(entryPL - exitPL, 2) * 100);
                    
                    TradesDataSet.TradesRow newTradesRoww = tradesDataSet.Trades.NewTradesRow();

                    newTradesRoww.Datestamp = textBoxDate.Text;
                    newTradesRoww.Instrument = comboBoxInstrument.Text;
                    newTradesRoww.Position = comboBoxPosition.Text;
                    newTradesRoww.Strategy = comboBoxStrategy.Text;
                    newTradesRoww.EntryPrice = textBoxEntryPrice.Text;
                    newTradesRoww.FinalExitPrice = textBoxFinalExitPrice.Text;
                    newTradesRoww.Pips = convertedShortValue;
                    tradesDataSet.Trades.Rows.Add(newTradesRoww);
                    break;
            }
            try
            {
                this.Validate();
                this.tradesBindingSource.EndEdit();
                this.tradesTableAdapter.Update(this.tradesDataSet.Trades);
                MessageBox.Show("Update successful!");
            }
            catch (System.Exception ex)
            {
                MessageBox.Show("Exception " + ex);
            }
        }

        public void computeFourDigitValues()
        {

            // Convert our Entry and Exit prices to a float
            float entryPL = float.Parse(textBoxEntryPrice.Text);
            float exitPL = float.Parse(textBoxFinalExitPrice.Text);                       

            // If the position is long, we have to do custom            
            switch (comboBoxPosition.Text)
            {
                case "Long":
                    string convertedLongValue = Convert.ToString(Math.Round(exitPL - entryPL, 4) * 10000);

                    TradesDataSet.TradesRow newTradesRow = tradesDataSet.Trades.NewTradesRow();
                    
                    newTradesRow.Datestamp = textBoxDate.Text;
                    newTradesRow.Instrument = comboBoxInstrument.Text;
                    newTradesRow.Position = comboBoxPosition.Text;
                    newTradesRow.Strategy = comboBoxStrategy.Text;
                    newTradesRow.EntryPrice = textBoxEntryPrice.Text;
                    newTradesRow.FinalExitPrice = textBoxFinalExitPrice.Text;
                    newTradesRow.Pips = convertedLongValue;
                    tradesDataSet.Trades.Rows.Add(newTradesRow);
                    break;
                case "Short":
                    string convertedShortValue = Convert.ToString(Math.Round(entryPL - exitPL, 4) * 10000);
                    
                    TradesDataSet.TradesRow newTradesRoww = tradesDataSet.Trades.NewTradesRow();

                    newTradesRoww.Datestamp = textBoxDate.Text;
                    newTradesRoww.Instrument = comboBoxInstrument.Text;
                    newTradesRoww.Position = comboBoxPosition.Text;
                    newTradesRoww.Strategy = comboBoxStrategy.Text;
                    newTradesRoww.EntryPrice = textBoxEntryPrice.Text;
                    newTradesRoww.FinalExitPrice = textBoxFinalExitPrice.Text;
                    newTradesRoww.Pips = convertedShortValue;
                    tradesDataSet.Trades.Rows.Add(newTradesRoww);
                    break;
            }
            try
            {
                this.Validate();
                this.tradesBindingSource.EndEdit();
                this.tradesTableAdapter.Update(this.tradesDataSet.Trades);                
            }
            catch (System.Exception ex)
            {
                MessageBox.Show("Exception " + ex);
            }
        }


        private void btnSubmit_Click(object sender, EventArgs e)
        {
            // Need to add validation to match exact numbers for entry and exit
            // e.g. 1.4344 or 143.22
            if (new Regex(@"\.\d{2}$").IsMatch(textBoxEntryPrice.Text))                
            {
                // Call our two digit function
                computeTwoDigitValues();
            }
            else
            {
                // Call our four digit function
                computeFourDigitValues();
            }
        }

        private void exitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            // Gracefully exit the application
            Application.Exit();
        }

       

        private void btnModify_Click(object sender, EventArgs e)
        {
            if (tradesDataSet.HasChanges())
            {                
                try
                {
                    this.Validate();
                    this.tradesBindingSource.EndEdit();
                    this.tradesTableAdapter.Update(this.tradesDataSet.Trades);
                    MessageBox.Show("Update successful!");
                }
                catch (System.Exception ex)
                {
                    MessageBox.Show("Exception " + ex);
                }                
            }
            else
            {
                MessageBox.Show("You haven't modified anything.", "Oops", MessageBoxButtons.OK);
            }

            this.tradesTableAdapter.Fill(this.tradesDataSet.Trades);
        }       
    }
}